<html>
<head><meta charset="utf-8"><title>How to get non-monomorphised identifiers from MIR? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html">How to get non-monomorphised identifiers from MIR?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216511233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216511233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216511233">(Nov 12 2020 at 18:51)</a>:</h4>
<p>How do I get the non-monomorphized name of a function from a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/terminator/struct.Terminator.html"><code>rustc_middle::mir::terminator::Terminator</code></a> (whose kind is an <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/terminator/enum.TerminatorKind.html"><code>Operand::Call</code></a>?<br>
If I tried to pretty print the <code>Operand::Call</code>, I get things like <code>m1::foo2::&lt;F&gt;</code>, <code>&lt;F as std::ops::Fn&lt;()&gt;&gt;::call</code> or <code>m1::foo3::&lt;fn() {m2::bar}&gt;</code> instead of respectively <code>m1::foo2</code>, <code>Fn::call</code> and <code>m1::foo3</code>.<br>
I would like to have the same output than what I get with HIR: <code>tcx.def_path_str(my_function.to_def_id())</code></p>



<a name="216511402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216511402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216511402">(Nov 12 2020 at 18:52)</a>:</h4>
<p>I guess the issue is that what you want is the generic param?</p>



<a name="216511982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216511982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216511982">(Nov 12 2020 at 18:56)</a>:</h4>
<p>from <code>m1::foo3::&lt;fn() {m2::bar}&gt;</code>, I don't need <code>m1::foo&lt;T: Fn()&gt;</code>, I just need <code>m1::foo</code></p>



<a name="216513273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216513273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216513273">(Nov 12 2020 at 19:05)</a>:</h4>
<p>Is there a way to pretty print the real type of a hierarchy of enum (ie, not using <code>Debug</code>, but getting something like <code>typeof(my_enum).to_string()</code>)?</p>



<a name="216525951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216525951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216525951">(Nov 12 2020 at 20:46)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">func</span><span class="p">.</span><span class="n">ty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">body</span><span class="p">,</span><span class="w"> </span><span class="n">tcx</span><span class="p">).</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">ty</span>::<span class="n">FnDef</span><span class="p">(</span><span class="n">def_id</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_str</span><span class="p">(</span><span class="n">def_id</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* no path available (e.g., a function pointer call) */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="216577147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216577147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216577147">(Nov 13 2020 at 07:56)</a>:</h4>
<p>I had to modify it slightly, but it works, thank you so much!</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">subfunction</span><span class="p">.</span><span class="n">constant</span><span class="p">().</span><span class="n">unwrap</span><span class="p">().</span><span class="n">literal</span><span class="p">.</span><span class="n">ty</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">rustc_middle</span>::<span class="n">ty</span>::<span class="n">FnDef</span><span class="p">(</span><span class="n">def_id</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_str</span><span class="p">(</span><span class="o">*</span><span class="n">def_id</span><span class="p">));()},</span><span class="w"></span>
<span class="w">    </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* no path available (e.g., a function pointer call) */</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="216577874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20get%20non-monomorphised%20identifiers%20from%20MIR%3F/near/216577874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20get.20non-monomorphised.20identifiers.20from.20MIR.3F.html#216577874">(Nov 13 2020 at 08:07)</a>:</h4>
<p>Is there a better way to detect if the call was monomorphised than this?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">callee</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">rustc_middle</span>::<span class="n">ty</span>::<span class="n">FnDef</span><span class="p">(</span><span class="n">def_id</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">subfunction</span><span class="p">.</span><span class="n">constant</span><span class="p">().</span><span class="n">unwrap</span><span class="p">().</span><span class="n">literal</span><span class="p">.</span><span class="n">ty</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">tcx</span><span class="p">.</span><span class="n">def_path_str</span><span class="p">(</span><span class="o">*</span><span class="n">def_id</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">unreachable</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">monomorphized_call</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">subfunction</span><span class="p">)</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">callee</span><span class="p">;</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>